<template>
  <div>
    <el-form class="input-form"
             label-position="right"
             label-width="120px"
             ref="editForm"
             :rules="propRules"
             :disabled="propReadonly"
             :model="form"
    >
      <el-collapse v-model="activeNames">
        <!--基本信息-->
        <el-collapse-item title="基本信息" name="1">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="单号" prop="busKey">
                <span>{{strings.defaultString(form.busKey,'自动设置')}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="申请日期" prop="busDate">
                <span>{{strings.defaultString(form.busDate,'自动设置')}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="申请人" prop="busUser">
                <span>{{strings.defaultString(form.busUser,'自动设置')}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="申请科室" prop="deptName">
                <span>{{strings.defaultString(form.deptName,'自动设置')}}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item label="询价开始时间" prop="bidStartDate">
               {{form.bidStartDate}}
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="询价结束时间" prop="bidEndDate">
                {{form.bidEndDate}}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="备注" prop="remark">
               {{form.remark}}
              </el-form-item>
            </el-col>
          </el-row>
        </el-collapse-item>

        <!--申请材料-->
        <el-collapse-item title="申请材料" name="3">
          <FortenUpload
            :title="'请上传'"
            :fileRefTable="'adm_matr_apply'"
            :fileSavePath="'adm_matr_apply'"
            :fileRefId="form.id"
            :propStatus="propStatus"
            :fileType="'all'"
            :fileMaxNum="999"
            :listHeight="'80px'"
            :noTitle="true"
            :readonly="true"
          ></FortenUpload>
        </el-collapse-item>

        <!--耗材添加-->
        <el-collapse-item title="申请耗材列表" name="4">
          <data-view
            propViewKey="adm_matr_apply_item_view"
            propViewForm="adm/matrapplyitem/detail.vue"
            :propInitData="form"
            :propAutoFetchData="false"
            :propHideFlowSelect="true"
            ref="childDataViewApplyItem"
            class="data-view-inlinebtn"
            @before-button-click="beforeButtonClick"
          >
          </data-view>
        </el-collapse-item>

        <!--供应商询价结果-->
        <el-collapse-item title="供应商询价结果" name="51">
        </el-collapse-item>

        <!--中标结果-->
        <el-collapse-item title="中标结果" name="52">
        </el-collapse-item>
      </el-collapse>
    </el-form>
  </div>
</template>

<script>
/* eslint-disable*/
import FortenUpload from '@/components/common/FortenUpload.vue'
export default {
  name: 'applyview',
  data() {
    return {
      form: {},
      viewData: {},
      activeNames: ['1', '3', '4', '5', '51', '52']
    }
  },
  components: {
    FortenUpload
  },
  methods: {
    //刷新申请材料
    fetchDataApplyItem() {
      //固定数据筛选参数
      let wrapper = this.$newWrapper().eq('applyId', this.form.id ? this.form.id : '-1')
      this.$refs.childDataViewApplyItem.setQueryWrapper(wrapper)
    },
    //开始日期选择后
    bidStartDateChange() {
      if (this.form.bidStartDate && !this.form.bidEndDate) this.$refs.bidEndDate.focus()
    },
    //按钮操作前
    async beforeButtonClick({btn}) {
      if(btn.code == 'vb-add'){ //添加前如果没保存，则先保存主表
        if (!this.form.id) {
          this.$refs.childDataViewApplyItem.setViewFormVisible(false)
          await this.propParent.ocAsynSaveViewForm()

          if (this.strings.isBlank(this.form.id)) {   //保存失败，排除服务器异常，则一般是校验失败
            if (!this.form.bidStartDate) this.$refs.bidStartDate.focus()
            if (this.form.bidStartDate && !this.form.bidEndDate) this.$refs.bidEndDate.focus()
          } else {
            this.$refs.childDataViewApplyItem.viewAdd()
          }
        }

      }
    },
    async getForm() {
      return this.form
    },
    getById() {
      this.$ajax.get('/adm/matr_apply/find?id=' + this.propInitData.applyId, {},{
      }).then(resp => {
        if (resp.code == 0) {
          this.form = resp.data
          this.fetchDataApplyItem()
        }
      })
    }
  },
  created() {
    let that = this
    this.$emit('event-register', {
      validate: (callback) => {
        this.$refs.editForm.validate(callback)
      },
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
        // 设置switch 元素默认值
      },
      afterSave: () => {
        that.propParent.fetchData()
      },
      beforeDelete: () => {
        return true
      },
      afterDelete: () => {
      },
      beforeFlow: (flowButtonKey) => {
        return true
      },
      afterFlow: (flowButtonKey) => {
      },
      beforeClose: () => {
        return true
      }
    })



  },
  props: {
    propParent: Object,
    propInitData: Object,
    propPrimaryKey: String,
    propDictionaries: Object,
    propCurrentRow: Object,
    propViewFormData: Object,
    propReadonly: Boolean,
    propStatus: String,
    propRules: Object,
    applyId: null
  },
  watch:{
    //视图状态变化后，显示上传操作
    propInitData:{
      handler(newValue, oldValue) {
        if(this.propInitData.applyId){
         this.getById()
        }
      },
      deep: true,
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>
  .container {
    width: 100%;
    height: 100%;
  }
</style>
